const { app, BrowserWindow, globalShortcut } = require("electron");
require("@electron/remote/main").initialize();
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";

// 实现热部署
const isDevelopment = !app.isPackaged;
if (isDevelopment) {
  try {
    require("electron-reloader")(module);
  } catch (err) {}
}

const createWindow = () => {
  const win = new BrowserWindow({
    width: 350,
    height: 250,
    frame: false,
    // transparent: true,
    webPreferences: {
      // 开启node环境
      nodeIntegration: true,
      contextIsolation: false,
    },
  });
  // 注册全局快捷键Ctrl+B的监听器（只要应用打开，该快捷键就会被监听，监听到就会执行下面的操作）
  // globalShortcut.register("Control+B", () => {
  //   // console.log('Control+B is pressed')
  //   const win = new BrowserWindow({
  //     width: 800,
  //     height: 600,
  //     webPreferences: {
  //       // 开启node环境
  //       nodeIntegration: true,
  //       contextIsolation: false,
  //     },
  //   });
  //   win.loadURL("http://www.baidu.com");
  // });
  // 检查快捷键是否注册成功
  // console.log(globalShortcut.isRegistered("Control+B"));
  // 加载remote模块
  require("@electron/remote/main").enable(win.webContents);
  // 加载主界面
  win.loadFile("demo11.html");
};

app.whenReady().then(() => {
  createWindow();

  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});

// app.on("will-quit", () => {
//   // 注销快捷键
//   globalShortcut.unregister("Control+B");
//   // 注销所有快捷键
//   globalShortcut.unregisterAll();
// });
